package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//根据分类id查询菜品
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐管理页面")
public class SetmealController {
    @Autowired
    private SetmealService setMealService;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "Setmealcache",key = "#setmealDTO.categoryId")
    public Result insert(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增菜品");
        setMealService.insert(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询套餐")
    public Result<PageResult> selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        log.info("分页查询套餐");
        PageResult<Setmeal> setmealPageResult = setMealService.selectPage(setmealPageQueryDTO);
        return Result.success(setmealPageResult);
    }

    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "Setmealcache",allEntries = true)
    @ApiOperation("修改菜品状态")
    public Result updateStatus(@PathVariable Integer status, Long id) {

        log.info("修改菜品状态");
        setMealService.updateStatus(status, id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品信息")
    @CacheEvict(cacheNames = "Setmealcache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {

        log.info("修改菜品信息");
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("回显操作")
    public Result<SetmealVO> selectById(@PathVariable Long id) {

        log.info("回显操作");
        SetmealVO setmealVO = setMealService.selectById(id);

        return Result.success(setmealVO);
    }

    @DeleteMapping
    @ApiOperation("批量删除 ")
    @CacheEvict(cacheNames = "Setmealcache",allEntries = true)
    public Result delect(@RequestParam List<Long> ids) {
        log.info("批量删除");
        setMealService.delect(ids);
        return Result.success();
    }
}
